package com.source.chart.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.source.baseData.service.PoultryCategoryService;
import com.source.chart.VO.*;
import com.source.chart.mapper.PoultrySalesMapper;
import com.source.chart.service.PoultrySalesChartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Month;
import java.util.*;
import java.util.concurrent.ExecutorService;

/**
 * @author fengq
 */
@Service
public class PoultrySalesChartServiceImpl extends ServiceImpl<PoultrySalesMapper, PoultrySalesVO> implements PoultrySalesChartService {

    @Autowired
    private PoultrySalesMapper poultrySalesMapper;
    @Autowired
    private PoultryCategoryService poultryCategoryService;

    @Override
    public List<PoultrySalesVO> findAllSales(int house_id) {
        return poultrySalesMapper.findAllSalesByHouse(house_id);
    }

    @Override
    public Map<String, BigInteger> findSalesInYears(String parentId, String houseId) {
        Map<String, BigInteger> result = new HashMap<>();
        List<String> idList = poultrySalesMapper
                .findIdByParentAndHouseId(parentId, houseId);
        if (null != idList) {
            List<Object> countOfPoultryInYear = poultrySalesMapper
                    .findCountOfPoultryInYear(idList);
            if (null != countOfPoultryInYear || countOfPoultryInYear.size()>0) {
                for (Object object : countOfPoultryInYear) {
                    Object[] ob = (Object[]) object;
                    result.put((String)ob[1], (BigInteger) ob[0]);
                }
            }
        }
        return result;
    }

//    @Override
//    public HashMap<String, Long> findCountOfParentInYear(String houseId) {
//
//        List<PoultrySales> countOfParentInYearByHouseId = poultrySalesMapper.findCountOfParentInYearByHouseId(houseId);
//        HashMap<String, Long> res = new HashMap<String, Long>();
//        res.put("鸡",0L);
//        res.put("鸭",0L);
//        res.put("鹅",0L);
//        res.put("其它",0L);
//        for (PoultrySales poultrySales : countOfParentInYearByHouseId) {
//            res.put(poultrySales.getPoultryParentName(),poultrySales.getCount());
//        }
//
//        return res;
//    }

    @Override
    public PoultrySalesChart findCountOfParentByHouseId(String houseId) {
        Map<String,Long> map=new HashMap<>();
        map.put("0",0L);
        map.put("1",0L);
        map.put("2",0L);
        map.put("3",0L);

        List<PoultryParentCountVo> list = poultrySalesMapper.findCountOfParentByHouseId(houseId);
        long total=0L;
        for (PoultryParentCountVo poultryParentCountVo:list){
            map.put(poultryParentCountVo.getParentId(),poultryParentCountVo.getCount());
            total=total+poultryParentCountVo.getCount();
        }
        map.put("sum",total);
        List<PoultryParentMapVo> poultryParentMapVos = poultrySalesMapper.selectPoultryParentMapVo();
        PoultrySalesChart poultrySalesChart = new PoultrySalesChart();
        poultrySalesChart.setPoultryParentMapVos(poultryParentMapVos);
        List<Map<String,Long>> mapList=new ArrayList<>();
        mapList.add(map);
        poultrySalesChart.setMapList(mapList);
        return poultrySalesChart;
    }

    @Override
    public List<PoultrySpeciesCountChart> selectPoultryCategoryCountOfYear(String houseId) {
        return poultrySalesMapper.selectPoultryCategoryCountOfYear(houseId);
    }

    @Override
    public List<PoultrySpeciesCountChart> selectPoultryCategoryCountOfAll(String houseId) {
        return poultrySalesMapper.selectPoultryCategoryCountOfAll(houseId);
    }

    @Override
    public PoultrySalesCountChart getPoultryCateCountOfMoth(String houseId) {
        List<PoultryParentMapVo> poultryParentMapVos = poultrySalesMapper.selectPoultryParentMapVo();
        List<String> poultryParent=new ArrayList<>();
        for (PoultryParentMapVo poultryParentMapVo:poultryParentMapVos){
            poultryParent.add(poultryParentMapVo.getPoultryParentName());
        }
        List<List<Long>> countList=new ArrayList<>();
        List<Integer> months=new ArrayList<>();
        String date = new SimpleDateFormat("yyyy-").format(new Date());
        int month = LocalDate.now().getMonth().getValue();
        for (Integer i=1;i<=month;i++){
            months.add(i);
            List<Long> count=Arrays.asList(0L,0L,0L,0L);
            String time = date.concat(i.toString());
            List<PoultrySpeciesCountChart> list = poultrySalesMapper.selectPoultryCateCountOfMoth(houseId, time);
            for (String s:poultryParent){
                for (PoultrySpeciesCountChart poultrySpeciesCountChart:list){
                    if (s.equals(poultrySpeciesCountChart.getPoultryParentName())){
                        count.add(poultryParent.indexOf(s),poultrySpeciesCountChart.getCount());
                    }
                }
            }
            countList.add(count);
        }
        PoultrySalesCountChart poultrySalesCountChart = new PoultrySalesCountChart();
        poultrySalesCountChart.setSales_date(countList);
        poultrySalesCountChart.setParent(poultryParent);
        poultrySalesCountChart.setMonth(months);
        return poultrySalesCountChart;
    }
}
